﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
using WPFExcelReport;
using System.IO;
using System.IO.Ports;
namespace WPFExcelReport
{
    using zl_u8 = Byte;
    using zl_u16 = System.UInt16;
    using zl_u32 = System.UInt32;
    using zl_s8 = SByte;
    using zl_s16 = System.Int16;
    using zl_s32 = System.Int32;
    using zl_bool = System.Int32;
    using zl_boolean = System.UInt32;
    using zl_char = System.SByte;
    using zl_int = System.Int32;
    using IP_ADDR = System.UInt32;
    using ZLDevHandler = System.UInt32;
    public partial class TZLFX0N
    {

        public TZLFX0N()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_ZLAN();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            txtWrite0.Text = "2";
            txtWrite1.Text = "3";
            txtWrite2.Text = "4";
            txtWrite3.Text = "5";
            txtWrite4.Text = "6";
        }
        SerialPort _com;
        SerialPort com
        {
            get
            {
                if (_com == null)
                {
                    _com = new SerialPort("COM1", 9600, Parity.Even, 7, StopBits.One);
                    _com.Open();
                }

                return _com;
            }
        }

        private void OpenCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (com.IsOpen)
                {
                    com.Close();
                }
                com.Open();
            }
            catch (Exception ex)
            {
                Helpers.LogHelper.error(ex.StackTrace + ex.Message);
            }
        }
        private void CloseCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                com.Close();
            }
            catch (Exception ex)
            {
                Helpers.LogHelper.error(ex.StackTrace + ex.Message);
            }
       
        }

        private void btnWrite_Click(object sender, EventArgs e)
        {
            try
            {
                if (!com.IsOpen)
                    com.Open();
                string[] write = new string[] { txtWrite0.Text, txtWrite1.Text, txtWrite2.Text, txtWrite3.Text }; //将准备写入PLC的值
                                                                                                                  //将要写入的值转换成16进制数,补齐两个字节,注意高低字节需要交换
                string sWriteData = "";
                for (int i = 0; i < write.Length; i++)
                {
                    int val = Convert.ToInt32(write[i].Length > 0 ? write[i] : "0");
                    string s = val.ToString("X");
                    while (s.Length < 4)
                    {
                        s = "0" + s;
                    }
                    sWriteData += s.Substring(2, 2) + s.Substring(0, 2);
                }
                string cmd = Helper.PrepareCommd("1", "1190", "08", sWriteData);
                 
                Helpers.LogHelper.info("PLCData" + sWriteData);
                //写入命令,1表示写入,1194表示D202这个地址的16进制,04表示D202,D203为4个BYTE,1194=(202*2)+4096的16进制数,至于用它表示D202的起始位置,三菱故意要这么麻烦了.
                //sWriteData = "1119008" + sWriteData + Chr(3);
                //chr(2)和chr(3)是构成命令的标志字符,然后加上校验和,命令组织完成
                //sWriteData = Chr(2) + sWriteData + SumCheck(sWriteData);
                Helpers.LogHelper.info("PLCCommand"+cmd);

                //写入串口
                // com.Write(cmd);
                byte[] data = System.Text.Encoding.ASCII.GetBytes(cmd);
                com.Write(data, 0, data.Length);
                com.Close();
            }
            catch (Exception ex)
            {
                Helpers.LogHelper.error(ex.StackTrace + ex.Message);
            }

        }
        ///<summary>
        ///发送温湿度数据
        ///</summary>
       
        private void btnRead_Click(object sender, EventArgs e)
        {
            try
            {
                if (!com.IsOpen)
                    com.Open();
                this.txtRead0.Text = "";
                string sReadData = "";

                //在读PLC中的数据之前,需要先发个指令给它,让它将数据发送到串口,下面的字符串中,chr(2),chr(3)为PLC命令的格式标志,0119404中,0表示读,
                //1194表示D202的起始地址,04表示读D202,D203两个字,共4个字节,66为0119404和chr(3)的校验和,向串口写入"读"命令,其实和向plc地址中写入数据是一样的,只是没有数据,用0表示读
                string cmd = Helper.PrepareCommd("0", "1190", "10", "");
                com.Write(cmd);
                sReadData = "";
                //等待1秒钟
                System.Threading.Thread.Sleep(1000);
                // 从串口读数据
                byte[] data = new byte[1024];
                com.Read(data, 0, 1024);
                com.Close();
                //如果首位为2,则表示数据有效.这里有个问题,在第二次读,第2位才为'2',第三次又是首位为2,需要再测试
                if (data[0] == 2)
                {
                    string sReceiveData = System.Text.Encoding.ASCII.GetString(data);
                    //MessageBox.Show(sReceiveData);
                    //解析命令,将读到的字符解析成数字,注意高低位的转换
                    for (int i = 1; i < 1024; i += 4)
                    {
                        string sLow = sReceiveData.Substring(i, 2);
                        string sHigh = sReceiveData.Substring(i + 2, 2);
                        //int res = Convert.ToInt32(sHigh)+ Convert.ToInt32(sLow);
                        int res = Convert.ToInt32(sHigh, 16) + Convert.ToInt32(sLow, 16);

                        this.txtRead0.Text += res.ToString() + ",";
                    }

                }
            }
            catch (Exception ex)
            {
                Helpers.LogHelper.error(ex.StackTrace + ex.Message);
            }
        } 
    }
}
